package com.yuanye.gwcit.widget;

/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

public class QrCodeDrawable extends Drawable {
    private Paint boundingRectPaint;
    private Paint contentRectPaint;
    private Paint contentTextPaint;
    private QrCodeViewModel qrCodeViewModel;
    private int contentPadding;
    private int textWidth;

    public QrCodeDrawable(QrCodeViewModel qrCodeViewModel) {
        this.qrCodeViewModel = qrCodeViewModel;

        boundingRectPaint = new Paint();
        boundingRectPaint.setStyle(Paint.Style.STROKE);
        boundingRectPaint.setColor(Color.GREEN);
        boundingRectPaint.setStrokeWidth(3F);
        boundingRectPaint.setAlpha(200);

        contentRectPaint = new Paint();
        contentRectPaint.setStyle(Paint.Style.FILL);
        contentRectPaint.setColor(Color.GREEN);
        contentRectPaint.setAlpha(255);

        contentTextPaint = new Paint();
        contentTextPaint.setColor(Color.WHITE);
        contentTextPaint.setAlpha(255);
        contentTextPaint.setTextSize(36F);

        contentPadding = 25;
        textWidth = (int) contentTextPaint.measureText(qrCodeViewModel.getQrContent());
    }


    @Override
    public void draw(Canvas canvas) {
        canvas.drawRect(qrCodeViewModel.getBoundingRect(), boundingRectPaint);
//        canvas.drawRect(
//                new Rect(
//                        qrCodeViewModel.getBoundingRect().left,
//                        qrCodeViewModel.getBoundingRect().bottom + contentPadding / 2,
//                        qrCodeViewModel.getBoundingRect().left + textWidth + contentPadding * 2,
//                        qrCodeViewModel.getBoundingRect().bottom + (int) contentTextPaint.getTextSize() + contentPadding
//                ),
//                contentRectPaint
//        );
//        canvas.drawText(
//                qrCodeViewModel.getQrContent(),
//                qrCodeViewModel.getBoundingRect().left + contentPadding,
//                qrCodeViewModel.getBoundingRect().bottom + contentPadding * 2,
//                contentTextPaint
//        );
    }

    @Override
    public void setAlpha(int alpha) {
        boundingRectPaint.setAlpha(alpha);
        contentRectPaint.setAlpha(alpha);
        contentTextPaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter colorFilter) {
        boundingRectPaint.setColorFilter(colorFilter);
        contentRectPaint.setColorFilter(colorFilter);
        contentTextPaint.setColorFilter(colorFilter);
    }

    @Deprecated
    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
}
